﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Configuration;
using System.Web.Configuration;
using System.Web.UI;
using System.Web;
using System.Diagnostics;



namespace XMLPerformace
{

    internal class EncryptHelper
    {
        private static int depth = -1;

        /// <summary>
        /// Gets the string value from node.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="nameNode">The name node.</param>
        /// <returns></returns>
        ///
        public static string GetStringValueFromXML(string filePath)
        {
            String result = string.Empty;
            if (!File.Exists(filePath))
            {
                MessageBox.Show("File not exists!");
            }

            StreamReader streamReader = new StreamReader(filePath);
            result = streamReader.ReadToEnd();

            return result;
        }


        private static string TabOutput(int tabCount)
        {
            string t = string.Empty;
            for (int i = 0; i < tabCount; i++)
            {
                t += "\t";
            }
            return t;
        }

        /// <summary>
        /// Protects the section.
        /// </summary>
        /// <param name="sectionName">Name of the section.</param>
        /// <param name="provider">The provider.</param>
        /// 
        public static void ProtectSection(string sectionName,
                                   string provider)
        {
            if (provider == null)
            {
                provider = "DataProtectionConfigurationProvider";
            }
            Configuration config =
                WebConfigurationManager.
                    OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);




            ConfigurationSection section =
                         config.GetSection(sectionName);

            if (section != null &&
                      !section.SectionInformation.IsProtected)
            {
                section.SectionInformation.ProtectSection(provider);
                config.Save();
            }
        }

        /// <summary>
        /// Uns the protect section.
        /// </summary>
        /// <param name="sectionName">Name of the section.</param>
        public static void UnProtectSection(string sectionName)
        {
            Configuration config =
                WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

            ConfigurationSection section =
                      config.GetSection(sectionName);

            if (section != null &&
                  section.SectionInformation.IsProtected)
            {
                section.SectionInformation.UnprotectSection();
                config.Save();
            }
        }


        public static void Ecrypt(string fileToolPath, string fileConfigPath,
            List<string> listSection, bool isEncrupt)
        {

            FileInfo fileInfoConfig = new FileInfo(fileConfigPath);
            string directoryFileConfig = fileInfoConfig.FullName.Remove(fileInfoConfig.FullName.Length - (fileInfoConfig.Name.Length+1));

            if (isEncrupt == true)
            {
                FileInfo file = new FileInfo(fileConfigPath);
                
                const string fileFinal = "web.config";
                string path = file.FullName.Remove(file.FullName.Length - file.Name.Length);
                string fileNewFinal = string.Empty;
                
                if (file.Name != fileFinal)
                {
                    fileNewFinal = file.FullName.Remove(file.FullName.Length - file.Name.Length) + fileFinal;
                    file.MoveTo(fileNewFinal);
                }
                if (File.Exists(String.Format("{0}backup.config", path)))
                    File.Delete(String.Format("{0}backup.config", path));
                file.CopyTo(String.Format("{0}backup.config", path));
                file = new FileInfo(path + fileFinal);
                file.Refresh();
               

                //File file = File.Create(path + fileFinal);
                //file.SetAccessControl(new System.Security.AccessControl.FileSecurity())
                
            }

            foreach (string tempSection in listSection)
            {
                string arguments;
                if (isEncrupt == true)
                    //arguments = String.Format("-pef {0} . -prov DataProtectionConfigurationProvider", tempSection);
                    arguments = String.Format(@"-pef ""{0}"" . -prov DataProtectionConfigurationProvider", tempSection);
                else
                    arguments = String.Format(@"-pdf ""{0}"" ""{1}""", tempSection, directoryFileConfig);

                Process p1 = new Process();
                p1.StartInfo.FileName = fileToolPath;
                p1.StartInfo.Arguments = arguments; // Tham so mo file theo sau,rat quan trong nhe
                //p.StartInfo.UseShellExecute = false;
                //p.StartInfo.RedirectStandardOutput = true;
                p1.Start();
                p1.WaitForExit(); //Bo comment dong nay neu muon ung dung lam viec tiep
            
                p1.Close();
            }
        }
    }
}
